<?php

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Templatemodel extends CI_Model {

	protected $_CI;

	public static $searchField = array(
		'project_id' => 'project_id',
		'template_id' => 'template_id',
	);
	public static $likeField = array(
		'template_name' => 'template_name',
		'template_struct' => 'template_struct',
	);

	public static $sortField = array(
		'template_id' => 'template_id',
		'template_time_lastmodified' => 'template_time_lastmodified',
	);

	public function __construct(){
		parent::__construct();
		$this->_CI = & get_instance();
	}
/*
| -------------------------------------------------------------------
|  GET Functions
| -------------------------------------------------------------------
*/
	public function getTemplate($template_id){
		$this->db->from('template');
		$this->db->where('template_id',$template_id);
		$this->db->limit(1);
		$query = $this->db->get();
		$item = $query->row_array();

		return $item;
	}

	public function getTemplateListSection($project_ids,$assist){
		$this->db->select('template_id,template_struct,project_id,template_title,user_id,template_name,template_time_lastmodified');
		$this->db->from('template');
		$this->db->where_in('project_id',$project_ids);
		if(!empty($assist['where'])) $this->db->where($assist['where']);
		if(!empty($assist['like'])) $this->db->like($assist['like']);
		if(!empty($assist['sort'])) $this->db->order_by($assist['sort']['key'],$assist['sort']['dir']);
		$count = $this->db->count_all_results('',SQL_EXECUTE_RETAIN_CONDITION);

		$this->db->order_by('template_time_lastmodified','desc');
		if(!empty($assist['pagination'])) $this->db->limit($assist['pagination']['limit'],$assist['pagination']['start']);
		$query = $this->db->get();
		$list = $query->result_array();

		return array($list,$count);
	}

	public function getTemplateListByProject($project_id){
		$this->db->from('template');
		$this->db->where('project_id',$project_id);
		$query = $this->db->get();
		$list = $query->result_array();

		return $list;
	}

	public function getAllTemplateWidgetListByProjectID($project_id,$widget_name){
		
		$this->db->from('template');
		$this->db->where('project_id',$project_id);
		$this->db->like('template_struct', '<widget>'.$widget_name.'</widget>');
		
		return $this->db->count_all_results();
		
	}
/*
| -------------------------------------------------------------------
|  CREATE Functions
| -------------------------------------------------------------------
*/
	public function createTemplate($info){
		if(empty($info['template_css'])) {
			$info['template_css'] = '';
		}
		if(empty($info['template_struct'])) {
			$info['template_struct'] = '';
		}
		$this->db->insert('template',$info);
		return $this->db->insert_id();
	}
/*
| -------------------------------------------------------------------
|  EDIT Functions
| -------------------------------------------------------------------
*/
	public function editTemplate($template_id,$info){
		// if(empty($info['template_css'])) {
		// 	$info['template_css'] = '';
		// }
		// if(empty($info['template_struct'])) {
		// 	$info['template_struct'] = '';
		// }
		$this->db->where('template_id',$template_id);
		$this->db->update('template',$info);
	}

	public function updateTemplateLastmodifiedByWidget($project_id,$widget_name,$user_id){
		$this->db->select('template_id');
		$this->db->from('template');
		$this->db->where('project_id',$project_id);
		$this->db->like('template_struct','<widget>'.$widget_name.'</widget>');
		$query = $this->db->get();
		$list = $query->result_array();
		$list = extractColumn($list,'template_id');
		$list[] = -1;

		$this->db->set('template_time_lastmodified',NOW);
		$this->db->set('user_id',$user_id);
		$this->db->where_in('template_id',$list);
		$this->db->update('template');
	}
/*
| -------------------------------------------------------------------
|  DELETE Functions
| -------------------------------------------------------------------
*/
	public function deleteTemplate($template_id){
		$this->db->where('template_id',$template_id);
		$this->db->delete('template');
		return true;
	}
/*
| -------------------------------------------------------------------
|  CHECK Functions
| -------------------------------------------------------------------
*/
	public function checkNameAvailable($name,$project_id,$template_id = 0){
		$this->db->from('template');
		$this->db->where('template_id !=',$template_id);
		$this->db->where('project_id',$project_id);
		$this->db->where('template_name',$name);
		$count = $this->db->count_all_results();

		return ($count == 0);
	}
}